home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MONITOR_CLIENT.PY < prev    next >
Encoding:
Python Source  |  2000-06-02  |  2.9 KB  |  127 lines

  1. # -*- Mode: Python; tab-width: 4 -*-
  2.  
  3. # monitor client, unix version.
  4.  
  5. import asyncore
  6. import asynchat
  7. import regsub
  8. import socket
  9. import string
  10. import sys
  11. import os
  12.  
  13. import md5
  14. import time
  15.  
  16. class stdin_channel (asyncore.file_dispatcher):
  17.     def handle_read (self):
  18.         data = self.recv(512)
  19.         if not data:
  20.             print '\nclosed.'
  21.             self.sock_channel.close()
  22.             try:
  23.                 self.close()
  24.             except:
  25.                 pass
  26.             
  27.         data = regsub.gsub ('\n', '\r\n', data)
  28.         self.sock_channel.push (data)
  29.  
  30.     def writable (self):
  31.         return 0
  32.  
  33.     def log (self, *ignore):
  34.         pass
  35.         
  36. class monitor_client (asynchat.async_chat):
  37.     def __init__ (self, password, addr=('',8023), socket_type=socket.AF_INET):
  38.         asynchat.async_chat.__init__ (self)
  39.         self.create_socket (socket_type, socket.SOCK_STREAM)
  40.         self.terminator = '\r\n'
  41.         self.connect (addr)
  42.         self.sent_auth = 0
  43.         self.timestamp = ''
  44.         self.password = password
  45.  
  46.     def collect_incoming_data (self, data):
  47.         if not self.sent_auth:
  48.             self.timestamp = self.timestamp + data
  49.         else:
  50.             sys.stdout.write (data)
  51.             sys.stdout.flush()
  52.  
  53.     def found_terminator (self):
  54.         if not self.sent_auth:
  55.             self.push (hex_digest (self.timestamp + self.password) + '\r\n')
  56.             self.sent_auth = 1
  57.         else:
  58.             print
  59.  
  60.     def handle_close (self):
  61.         # close all the channels, which will make the standard main
  62.         # loop exit.
  63.         map (lambda x: x.close(), asyncore.socket_map.values())
  64.  
  65.     def log (self, *ignore):
  66.         pass
  67.  
  68. class encrypted_monitor_client (monitor_client):
  69.     "Wrap push() and recv() with a stream cipher"
  70.  
  71.     def init_cipher (self, cipher, key):
  72.         self.outgoing = cipher.new (key)
  73.         self.incoming = cipher.new (key)
  74.  
  75.     def push (self, data):
  76.         # push the encrypted data instead
  77.         return monitor_client.push (self, self.outgoing.encrypt (data))
  78.  
  79.     def recv (self, block_size):
  80.         data = monitor_client.recv (self, block_size)
  81.         if data:
  82.             return self.incoming.decrypt (data)
  83.         else:
  84.             return data
  85.  
  86. def hex_digest (s):
  87.     m = md5.md5()
  88.     m.update (s)
  89.     return string.join (
  90.         map (lambda x: hex (ord (x))[2:], map (None, m.digest())),
  91.         '',
  92.         )
  93.  
  94. if __name__ == '__main__':
  95.     if len(sys.argv) == 1:
  96.         print 'Usage: %s host port' % sys.argv[0]
  97.         sys.exit(0)
  98.  
  99.     if ('-e' in sys.argv):
  100.         encrypt = 1
  101.         sys.argv.remove ('-e')
  102.     else:
  103.         encrypt = 0
  104.  
  105.     sys.stderr.write ('Enter Password: ')
  106.     sys.stderr.flush()
  107.     import os
  108.     try:
  109.         os.system ('stty -echo')
  110.         p = raw_input()
  111.         print
  112.     finally:
  113.         os.system ('stty echo')
  114.     stdin = stdin_channel (0)
  115.     if len(sys.argv) > 1:
  116.         if encrypt:
  117.             client = encrypted_monitor_client (p, (sys.argv[1], string.atoi (sys.argv[2])))
  118.             import sapphire
  119.             client.init_cipher (sapphire, p)
  120.         else:
  121.             client = monitor_client (p, (sys.argv[1], string.atoi (sys.argv[2])))
  122.     else:
  123.         # default to local host, 'standard' port
  124.         client = monitor_client (p)
  125.     stdin.sock_channel = client
  126.     asyncore.loop()
  127.